/*
 * @Author: '龙佳鑫' '1554845699@qq.com'
 * @Date: 2024-10-29 21:28:55
 * @LastEditors: '龙佳鑫' '1554845699@qq.com'
 * @LastEditTime: 2024-11-09 16:17:23
 * @FilePath: \lvyou-node-v3\app.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const Koa = require('koa')
const app = new Koa()
const json = require('koa-json')//用于将http响应的数据转换为json格式
const bodyParser = require('koa-bodyparser')//解析http请求的消息体
const router = require('@koa/router')()
const cors = require('@koa/cors')//允许跨域
const mongoose = require('mongoose')
const { addAliases } = require('module-alias')
// 配置别名
addAliases({
    '@': __dirname
})
// 数据库地址
const { BASE_URL } = require('@/config/Account')
// 统一返回给前端的接口数据格式:中间件
const responseHandler = require('@/config/result')
// 捕获错误的中间件
const errorHandler = require('@/config/abnormal')

app.use(cors())
app.use(json())
app.use(bodyParser())
app.use(responseHandler)
app.use(errorHandler)

//连接数据库
mongoose.connect(BASE_URL)
    .then(res => {
        console.log('数据库连接成功');
    })
    .catch(err => {
        console.log(err);
        console.log('数据库连接失败！！！');
    })

//--------------接口小程序端----------------
// 登录注册用户账号
const login = require('@/routes/user/index')
// 游记
const article = require('@/routes/article/index')
// 首页
const home = require('@/routes/home/index')
// 搜索游记
const search = require('@/routes/search-travel/index')
// 目的地
const found = require('@/routes/found/index')
// 旅游结伴
const companion = require('@/routes/companion/index')

//--------------接口后台管理端----------------
const pcapi = require('@/routes/pc-admin/index')
// 数据分析
const analysis = require('@/routes/pc-data-analysis/index')

router.use('/api', login)
router.use('/api', article)
router.use('/api', pcapi)
router.use('/api', home)
router.use('/api', search)
router.use('/api', found)
router.use('/api', companion)
router.use('/api', analysis)

app.use(router.routes()).use(router.allowedMethods())
app.listen(8900)
console.log('端口启动成功');